const contactModel = require('../../model/contact/contactModel')

class contactService{
  //获取所有招聘信息
  async getContact(req,res){
    const {results:data} = await contactModel.contactInfo()
    if(data.length != 0 ){
      return res.json({
        data:data,
        meta: {
          msg: '获取最新公告成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta:{
          msg: '获取最新公告失败',
          status: 404
        }
      })
    }
  }

  //分页获取招聘信息
  async getPageContact(req,res){
    let {pagenum,per_page} = req.query
    pagenum = parseInt(pagenum)
    per_page = parseInt(per_page)
    if(per_page === 1){
      pagenum = pagenum-1
    }else if(per_page === 2){
      pagenum = (pagenum-1)*2
    }else if(per_page === 5){
      pagenum = (pagenum-1)*5
    }else if(per_page === 10){
      pagenum = (pagenum-1)*10
    }
    const Info = await contactModel.contactInfo()
    const total_count = Info.results.length
    const {results:data} = await contactModel.contactPage(pagenum,per_page)
    if(data.length != 0){
      return res.json({
        total_count,
        pagenum:pagenum+1,
        per_page,
        data
      })
    }else{
      return res.json({
        meta:{
          msg: '公告查询失败',
          status: 404
        }
      })
    }
  }

  //ID获取招聘信息
  async getIDContact(req,res){
    let {id} = req.query
    console.log(id)
    const {results:data} = await contactModel.contactID(id)
    if(data.length != 0 ){
      return res.json({
        data:data,
        meta: {
          msg: '获取最新公告成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta:{
          msg: '获取最新公告失败',
          status: 404
        }
      })
    }
  }

  //增加招聘信息
  async postAddContact(req,res){
    let {num,content1,content2,email,title} = req.body
    console.log(req.body)
    const {results:data} = await contactModel.contactAdd(num,content1,content2,email,title)
    if(data.length !== 0 ){
      return res.json({
        meta:{
          msg: '添加新招聘成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '添加招聘失败',
          status: 404
        }
      })
    }
  }

  //修改招聘信息
  async postUpdateContact(req,res){
    let {num,content1,content2,email,title,id} = req.body
    const {results:data} = await contactModel.contactUpdate(num,content1,content2,email,title,id)
    if(data.length != 0){
      return res.json({
        meta:{
          msg: '修改用户信息成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '修改用户信息失败',
          status: 404
        }
      })
    }

  }

  //删除用户信息
  async postDeleteContact(req,res){
    let {id} = req.body
    const {results:data} = await contactModel.contactDelete(id)
    if(data.length != 0){
      return res.json({
        meta:{
          msg: '修改用户信息成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '修改用户信息失败',
          status: 404
        }
      })
    }
  }
}
module.exports = new contactService